﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Collections.ObjectModel;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Tasks;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;
using System.IO;
using System.Xml;

namespace PhoneAppProject1
{
    public partial class SNews_Detail : PhoneApplicationPage
    {
		private string Link = "";
        ApplicationBarIconButton m_appbarBtnAdd;
        ApplicationBarIconButton m_appbarBtnDel;
        private string URL, Title, Message, CreateDate, ImgURL = ""; 
      
        public SNews_Detail()
        {
            InitializeComponent();
            m_appbarBtnAdd = ApplicationBar.Buttons[0] as ApplicationBarIconButton;
            m_appbarBtnDel = ApplicationBar.Buttons[1] as ApplicationBarIconButton;
        }

        private void PhoneApplicationPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            //// Reteiav Data And Add to Control ////
            NavigationContext.QueryString.TryGetValue("URL", out URL);
            NavigationContext.QueryString.TryGetValue("Title", out Title);
            NavigationContext.QueryString.TryGetValue("Message", out Message);
            NavigationContext.QueryString.TryGetValue("CreateDate", out CreateDate);
            NavigationContext.QueryString.TryGetValue("ImgURL", out ImgURL);
			this.Link = URL;
            txtCreateDate.Text += CreateDate;
            txtMessage.Text = Message;
            PageTitle.Text = Title;
			try
			{
            	Img.Source = new BitmapImage(new Uri(ImgURL));
			}
			catch
			{
				
			}

            // Check it Saved ??? //
            try
            {
                IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
                IsolatedStorageFileStream isoStore = myIsolatedStorage.OpenFile("ItemSaved", FileMode.Open, FileAccess.Read);
                using (StreamReader reader = new StreamReader(isoStore))
                {
                    bool c = true;
                    while (!reader.EndOfStream)
                    {
                        if (Title == reader.ReadLine())
                        {
                            c = false;
                        }
                        reader.ReadLine(); reader.ReadLine();
                        reader.ReadLine(); reader.ReadLine();
                        reader.ReadLine();
                       
                    }
                    if (c == false)
                    {
                        m_appbarBtnDel.IsEnabled = true;
                        m_appbarBtnAdd.IsEnabled = false;
                    }
                }

               
            }
            catch (Exception err)
            {

            }
        	// TODO: Add event handler implementation here.
        }

        private void btnGotoWeb_Click(object sender, System.EventArgs e)
        {
            WebBrowserTask webbrowser = new WebBrowserTask();
            webbrowser.URL = this.Link;
            webbrowser.Show();
        	// TODO: Add event handler implementation here.
        }

        private void btnAdd_Click(object sender, System.EventArgs e)
        {
            m_appbarBtnDel.IsEnabled = true;
            m_appbarBtnAdd.IsEnabled = false;
            try
            {
                List<string> listObj = new List<string>();
                try
                {
                    IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
                    IsolatedStorageFileStream isoStore = myIsolatedStorage.OpenFile("ItemSaved", FileMode.Open, FileAccess.Read);
                    using (StreamReader reader = new StreamReader(isoStore))
                    {
                        while (!reader.EndOfStream)
                        {
                            listObj.Add(reader.ReadLine());
                        }
                        reader.Close();
                    }
                }
                catch (Exception err)
                {
                    //MessageBox.Show(err.Message);
                }
                var store = IsolatedStorageFile.GetUserStoreForApplication();
                using (StreamWriter writer = new StreamWriter(new IsolatedStorageFileStream
                ("ItemSaved", FileMode.Create, store)))
                {
                    writer.WriteLine(this.Title);
                    writer.WriteLine(this.ImgURL);
                    writer.WriteLine(this.URL);
                    writer.WriteLine(this.Message);
                    writer.WriteLine(this.CreateDate);
                    writer.WriteLine(this.Message);
                    for (int i = 0; i < listObj.Count; i++)
                    {
                        writer.WriteLine(listObj[i].ToString());
                    }
                    writer.Close();
      
                }
            }
            catch
            {

            }
        }

        private void btnDelete_Click(object sender, System.EventArgs e)
        {
            m_appbarBtnDel.IsEnabled = false;
            m_appbarBtnAdd.IsEnabled = true;

            #region Delete item
            #region ReadData 
            List<string> listObj = new List<string>();
            try
            {
                IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
                IsolatedStorageFileStream isoStore = myIsolatedStorage.OpenFile("ItemSaved", FileMode.Open, FileAccess.Read);
                using (StreamReader reader = new StreamReader(isoStore))
                {
                    while (!reader.EndOfStream)
                    {
                        string strTitle = reader.ReadLine();

                        if (strTitle == this.Title)
                        {
                            reader.ReadLine(); reader.ReadLine(); reader.ReadLine();
                            reader.ReadLine(); reader.ReadLine();
                        }
                        else
                        {
                            listObj.Add(strTitle);
                        }
                    }
                    reader.Close();
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
            #endregion
            #region AddData
            var store = IsolatedStorageFile.GetUserStoreForApplication();
            using (StreamWriter writer = new StreamWriter(new IsolatedStorageFileStream
                ("ItemSaved", FileMode.Create, store)))
            {
                for (int i = 0; i < listObj.Count; i++)
                {
                    writer.WriteLine(listObj[i].ToString());
                }
                writer.Close();

            }
            #endregion
            #endregion
        }
    }
}